import { Form } from 'antd';
import { Fragment, memo } from 'react';
import { Collapse } from '../../Collapse';
import { CommonField } from './field';
export function renderFormItem(column, key) {
    const { Component, props: _props, slot, block, render, renderBlock, ...args } = column;
    if (render) {
        return (<Form.Item {...args} key={`formItem-${key}`}>
        {render(Form.useFormInstance())}
      </Form.Item>);
    }
    if (renderBlock) {
        return renderBlock(Form.useFormInstance());
    }
    if (block) {
        return <Fragment key={`formItem-${key}`}>{block}</Fragment>;
    }
    return (<Form.Item key={`formItem-${key}`} {...args}>
      {slot ? slot : <CommonField Component={Component} props={_props}/>}
    </Form.Item>);
}
/**
 * form的统一入口
 */
export const CommonForm = memo(function (props) {
    const { columns, formProps, collapse } = props;
    if (formProps) {
        formProps.size = formProps.size || 'middle';
    }
    const items = columns.map((column, index) => renderFormItem(column, index));
    const formItem = collapse ? <Collapse {...collapse}>{items}</Collapse> : items;
    return <Form autoComplete='off' {...formProps}>{formItem}</Form>;
});
export default CommonForm;
